package serviceChargeTransaction;

import java.util.Date;

import addEmployeeTransaction.Employee;
import affiliation.UnionAffiliation;

import database.PayrollDatabase;

import main.Transaction;

public class ServiceChargeTransaction extends Transaction {

	private int memberId;
	private Date time;
	private double charge;

	public ServiceChargeTransaction(int id, Date time, double charge,
			PayrollDatabase database) {
		super(database);

		this.memberId = id;
		this.time = time;
		this.charge = charge;
	}

	@Override
	public void execute() throws Exception {
		Employee e = database.getUnionMember(memberId);

		if (e != null) {
			UnionAffiliation ua = null;
			if (e.getAffiliation() instanceof UnionAffiliation)
				ua = (UnionAffiliation) e.getAffiliation();

			if (ua != null) {
				ua.addServiceCharge(new ServiceCharge(time, charge));
			} else {
				throw new Exception(
						"Tries to add service charge to union member without a union affiliation");
			}
		} else {
			throw new Exception("No such union member.");
		}
	}
}
